#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    tcpSocket=new QTcpSocket(this);
    catSocket=new QTcpSocket(this);
    time=new QTimer(this);
    timeStop=new QTimer(this);
    this->setWindowTitle("小车控制台");
    this->setWindowIcon(QIcon(":/img/iocn.png"));

    QPixmap pixUp(":/img/up.png");//解除圆形按钮边缘可点击
    ui->up->resize(pixUp.size());
    ui->up->setIcon(pixUp);
    ui->up->setIconSize(pixUp.size());
    ui->up->setMask(pixUp.mask());

    QPixmap pixDown(":/img/down.png");
    ui->down->resize(pixDown.size());
    ui->down->setIcon(pixDown);
    ui->down->setIconSize(pixDown.size());
    ui->down->setMask(pixDown.mask());

    QPixmap pixLeft(":/img/left.png");
    ui->left->resize(pixLeft.size());
    ui->left->setIcon(pixLeft);
    ui->left->setIconSize(pixLeft.size());
    ui->left->setMask(pixLeft.mask());

    QPixmap pixRight(":/img/right.png");
    ui->right->resize(pixRight.size());
    ui->right->setIcon(pixRight);
    ui->right->setIconSize(pixRight.size());
    ui->right->setMask(pixRight.mask());

    connect(tcpSocket,&QTcpSocket::connected,[=]{
        qDebug("链接成功");
        ui->link->setText("断开");
        ui->state->setText(QString("状态：连接了"));
        QString s="send";
        tcpSocket->write(s.toUtf8().data());
    });
    connect(tcpSocket,&QTcpSocket::disconnected,[=]{
        qDebug("服务器断开连接");
        ui->state->setText(QString("状态：未连接"));
        ui->link->setText("链接");
        time->stop();
    });
    connect(tcpSocket,&QTcpSocket::readyRead,[=]{
        //QString str=tcpSocket->readAll();
        if(isIntIma){
            if(tcpSocket->bytesAvailable()<4){
                return;
            }
//            QString str=tcpSocket->readAll();
//            size=str.toInt();

            tcpSocket->read(buf_len, 32);
            size = atoi(buf_len);
            qDebug("size:%d",size);
            isIntIma=false;
        }
        else{
            if(tcpSocket->bytesAvailable()<size){
                return;
            }
            qDebug("加载图片中...");
            tcpSocket->read(img,size);
            QPixmap pix;
            pix.loadFromData((uchar *)img,size,"JPEG");
            ui->label->setScaledContents(true);
            ui->label->setPixmap(pix);
            isIntIma=true;
        }
    });

    connect(time,&QTimer::timeout,[=]{
        QString s="send";
        tcpSocket->write(s.toUtf8().data());
    });
    connect(timeStop,&QTimer::timeout,[=]{
        timeStop->stop();
        goStop();
    });


    connect(ui->up,&QPushButton::pressed,[=]{
        goUp();
    });
    connect(ui->down,&QPushButton::pressed,[=]{
        goDown();
    });
    connect(ui->left,&QPushButton::pressed,[=]{
        goLeft();
    });
    connect(ui->right,&QPushButton::pressed,[=]{
        goRight();
    });
    connect(ui->up,&QPushButton::released,[=]{
        goStop();
    });
    connect(ui->down,&QPushButton::released,[=]{
        goStop();
    });
    connect(ui->left,&QPushButton::released,[=]{
        goStop();
    });
    connect(ui->right,&QPushButton::released,[=]{
        goStop();
    });
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_link_clicked()
{
    if(ui->link->text()=="链接"){
        QString ip=ui->ipLine->text();
        qint16 port=ui->portLine->text().toInt();
        tcpSocket->connectToHost(ip,port);
        catSocket->connectToHost("192.168.1.1",6666);
        time->start(50);
    }else{
        goStop();
        tcpSocket->close();
        catSocket->close();
        this->close();
    }
}





void Widget::keyPressEvent(QKeyEvent *e)
{
    int key=e->key();
    if('W'==key){
        goUp();
    }else if('S'==key){
        goDown();
    }else if('A'==key){
        goLeft();
    }else if('D'==key){
        goRight();
    }else if(' '==key){
        goStop();
    }
}

void Widget::goUp()
{
    qDebug("W");
    QString s="up";
    catSocket->write(s.toUtf8().data());
}

void Widget::goDown()
{
    qDebug("S");
    QString s="down";
    catSocket->write(s.toUtf8().data());
}

void Widget::goLeft()
{
    qDebug("A");
    QString s="left";
    timeStop->start(360);
    catSocket->write(s.toUtf8().data());
}

void Widget::goRight()
{
    qDebug("D");
    QString s="right";
    timeStop->start(360);
    catSocket->write(s.toUtf8().data());
}

void Widget::goStop()
{
    qDebug("stop");
    QString s="stop";
    catSocket->write(s.toUtf8().data());
}

void Widget::on_yaogan_valueChanged(int value)
{
    qDebug("%d",value);
}
